//Project: OOR Phase 2
 //Author: Accenture IDC 
 //Class Name: scheduledOORUpdateLeads 
 //Description: Scheduled class for updating Lead Manager whenever there is a Update on Owner Manager  

global class scheduledOORUpdateLeads implements Schedulable{
    public static ID localAdsLeadRecordType = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
    
    List<User> userList = [SELECT Id,ManagerId,OORLeadManagerUpdate__c FROM User WHERE OORLeadManagerUpdate__c = True];
    Set<Id> idSet = new Set<Id>();//Set for passing UserId's to Dynamic Query 
    Map<Id,Id> userMap= New Map<Id,Id>();//Map for storing Key value pair(UserId and ManagerId)
    
    global void execute(SchedulableContext sc)
    {
        if(userList.size()>0)
        {
            for(User userObj : userList )
            {
            idSet.add(userObj.Id); 
            userMap.put(userObj.Id,userObj.ManagerId);   
            }
            OORUpdateLeads job= New OORUpdateLeads();
            job.userSetId=idSet;
            job.UserManagerMaplist=userMap;
            job.userList=userList;
            job.Query = 'Select Id,OwnerId,User_Manager__c from Lead where RecordTypeId =\''+localAdsLeadRecordType+ '\' AND  OwnerId IN:userSetId AND ISCONVERTED = FALSE';  
            Database.executeBatch(job, 10);    //Execute Instruction for Batch 
        }
    }
    //Test Method
    static testMethod void testscheduledOORUpdateLeads() 
    {
        scheduledOORUpdateLeads updateLeads = new scheduledOORUpdateLeads();
        test.startTest();
        String sch = '0 0 17 * * ?';          
        system.schedule('Scheduled OORUpdateLeads', sch, updateLeads);
        test.stopTest();
    }
}